package com.example.graduatetest.view.model;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Handler;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

import androidx.appcompat.widget.Toolbar;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.example.graduatetest.R;
import com.example.graduatetest.bean.Classes;
import com.example.graduatetest.bean.Notice;
import com.example.graduatetest.bean.Student;
import com.example.graduatetest.callback.StringCallBack;
import com.example.graduatetest.callback.StudentList;
import com.example.graduatetest.databinding.ActivityNoticeWriteBinding;
import com.example.graduatetest.model.ClassModel;
import com.example.graduatetest.model.NoticeModel;

import java.util.ArrayList;
import java.util.List;

public class NoticeWriteViewModel extends ViewModel {
    private Activity activity;
    private ActivityNoticeWriteBinding binding;
    private MutableLiveData<String> titleText;
    private MutableLiveData<String> sendPersonText;
    private MutableLiveData<String> contentText;
    private EditText edContent;
    private String label;
    private Classes ownClass;
    private String type;
    private Handler handler = new Handler();
    private List<Student> studentList = new ArrayList<>();
    //输入框初始值
    private int num = 0;
    //输入框最大值
    private int mMaxNum=500;

    public void init(Activity activity,ActivityNoticeWriteBinding binding,Classes ownClass,String type){
        this.activity = activity;
        this.binding = binding;
        this.ownClass = ownClass;
        this.type = type;
        if(titleText == null) titleText = new MutableLiveData<>();
        if(sendPersonText == null) sendPersonText = new MutableLiveData<>();
        if (contentText == null) contentText = new MutableLiveData<>();
        edContent = binding.edContent;
        toolbarInit();
        initView();
        initClick();
    }

    //初始化toolbar
    private void toolbarInit(){
        Toolbar toolbar = binding.toolbar14;
        toolbar.setTitle("编辑消息");
        toolbar.setNavigationIcon(R.drawable.ic_arrow_back_black_24dp);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                activity.onBackPressed();
            }
        });
    }

    private void initClick(){
        binding.submitButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SharedPreferences preferences = activity.getSharedPreferences("data", Context.MODE_PRIVATE);
                final String number = preferences.getString("number","2017211973");
                if(titleText.getValue() != null && sendPersonText.getValue() != null && contentText.getValue() != null){
                   networkWatch(titleText.getValue(),contentText.getValue(),sendPersonText.getValue(),number,label,ownClass.getId(),"NO");
                }else{
                    Toast.makeText(activity, "内容填写不完整", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
    //查看
    private void networkWatch(final String title, final String content, final String send_name, final String send_number, final String label, final String class_number, final String class_code){
        ClassModel.getStudentList(ownClass.getId(), new StudentList() {
            @Override
            public void onSuccess(List<Student> list) {
                studentList = list;
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        networkInsert(title, content, send_name, send_number, label, class_number, class_code,studentList);
                    }
                });
            }

            @Override
            public void onFailed() {
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(activity, "查询学生列表失败",Toast.LENGTH_SHORT).show();
                    }
                });
            }
        });

    }

    //插入记录
    private void networkInsert(String title,String content,String send_name,String send_number,String label,String class_number,String class_code,List<Student> list){
        for (Student student : list) {

            NoticeModel.setInsertOneNotice(title, content, send_name, send_number, student.getNumber(), label, class_number, class_code,"NO",new StringCallBack() {
                @Override
                public void onSuccess(final String result) {
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            if(result.equals("success")){
                                activity.onBackPressed();
                                Log.e("haha", "插入notice成功");
                            }
                        }
                    });
                }

                @Override
                public void onFailed() {
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(activity, "插入失败", Toast.LENGTH_SHORT).show();
                        }
                    });
                }
            });
        }

    }
    //初始化页面
    private void initView(){
        label = type;
        binding.textType.setText(label);
        binding.edContent.addTextChangedListener(new TextWatcher() {
            //记录输入的字数
            private CharSequence wordNum;
            private int selectionStart;
            private int selectionEnd;

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                //实时记录输入的字数
                wordNum= s;
            }

            @Override
            public void afterTextChanged(Editable s) {
                int number = num + s.length();
                //TextView显示剩余字数
                binding.textTextNumber.setText("" + number+"/500");
                selectionStart=edContent.getSelectionStart();
                selectionEnd = edContent.getSelectionEnd();
                //判断大于最大值
                if (wordNum.length() > mMaxNum) {
                    //删除多余输入的字（不会显示出来）
                    s.delete(selectionStart - 1, selectionEnd);
                    int tempSelection = selectionEnd;
                    edContent.setText(s);
                    edContent.setSelection(tempSelection);//设置光标在最后
                    //吐司最多输入300字
                    Toast.makeText(activity, "最多输入500字", Toast.LENGTH_SHORT).show();
                }
            }
        });

    }

    public MutableLiveData<String> getSendPersonText() {
        return sendPersonText;
    }

    public MutableLiveData<String> getTitleText() {
        return titleText;
    }

    public MutableLiveData<String> getContentText() {
        return contentText;
    }
}
